#!/data/data/com.termux/files/usr/bin/bash

_PROC_CAP_LAST_CAP='40'

_PROC_VM_STAT='nr_free_pages 1743136
nr_zone_inactive_anon 179281
nr_zone_active_anon 7183
nr_zone_inactive_file 22858
nr_zone_active_file 51328
nr_zone_unevictable 642
nr_zone_write_pending 0
nr_mlock 0
nr_bounce 0
nr_zspages 0
nr_free_cma 0
numa_hit 1259626
numa_miss 0
numa_foreign 0
numa_interleave 720
numa_local 1259626
numa_other 0
nr_inactive_anon 179281
nr_active_anon 7183
nr_inactive_file 22858
nr_active_file 51328
nr_unevictable 642
nr_slab_reclaimable 8091
nr_slab_unreclaimable 7804
nr_isolated_anon 0
nr_isolated_file 0
workingset_nodes 0
workingset_refault_anon 0
workingset_refault_file 0
workingset_activate_anon 0
workingset_activate_file 0
workingset_restore_anon 0
workingset_restore_file 0
workingset_nodereclaim 0
nr_anon_pages 7723
nr_mapped 8905
nr_file_pages 253569
nr_dirty 0
nr_writeback 0
nr_writeback_temp 0
nr_shmem 178741
nr_shmem_hugepages 0
nr_shmem_pmdmapped 0
nr_file_hugepages 0
nr_file_pmdmapped 0
nr_anon_transparent_hugepages 1
nr_vmscan_write 0
nr_vmscan_immediate_reclaim 0
nr_dirtied 0
nr_written 0
nr_throttled_written 0
nr_kernel_misc_reclaimable 0
nr_foll_pin_acquired 0
nr_foll_pin_released 0
nr_kernel_stack 2780
nr_page_table_pages 344
nr_sec_page_table_pages 0
nr_swapcached 0
pgpromote_success 0
pgpromote_candidate 0
nr_dirty_threshold 356564
nr_dirty_background_threshold 178064
pgpgin 890508
pgpgout 0
pswpin 0
pswpout 0
pgalloc_dma 272
pgalloc_dma32 261
pgalloc_normal 1328079
pgalloc_movable 0
pgalloc_device 0
allocstall_dma 0
allocstall_dma32 0
allocstall_normal 0
allocstall_movable 0
allocstall_device 0
pgskip_dma 0
pgskip_dma32 0
pgskip_normal 0
pgskip_movable 0
pgskip_device 0
pgfree 3077011
pgactivate 0
pgdeactivate 0
pglazyfree 0
pgfault 176973
pgmajfault 488
pglazyfreed 0
pgrefill 0
pgreuse 19230
pgsteal_kswapd 0
pgsteal_direct 0
pgsteal_khugepaged 0
pgdemote_kswapd 0
pgdemote_direct 0
pgdemote_khugepaged 0
pgscan_kswapd 0
pgscan_direct 0
pgscan_khugepaged 0
pgscan_direct_throttle 0
pgscan_anon 0
pgscan_file 0
pgsteal_anon 0
pgsteal_file 0
zone_reclaim_failed 0
pginodesteal 0
slabs_scanned 0
kswapd_inodesteal 0
kswapd_low_wmark_hit_quickly 0
kswapd_high_wmark_hit_quickly 0
pageoutrun 0
pgrotated 0
drop_pagecache 0
drop_slab 0
oom_kill 0
numa_pte_updates 0
numa_huge_pte_updates 0
numa_hint_faults 0
numa_hint_faults_local 0
numa_pages_migrated 0
pgmigrate_success 0
pgmigrate_fail 0
thp_migration_success 0
thp_migration_fail 0
thp_migration_split 0
compact_migrate_scanned 0
compact_free_scanned 0
compact_isolated 0
compact_stall 0
compact_fail 0
compact_success 0
compact_daemon_wake 0
compact_daemon_migrate_scanned 0
compact_daemon_free_scanned 0
htlb_buddy_alloc_success 0
htlb_buddy_alloc_fail 0
cma_alloc_success 0
cma_alloc_fail 0
unevictable_pgs_culled 27002
unevictable_pgs_scanned 0
unevictable_pgs_rescued 744
unevictable_pgs_mlocked 744
unevictable_pgs_munlocked 744
unevictable_pgs_cleared 0
unevictable_pgs_stranded 0
thp_fault_alloc 13
thp_fault_fallback 0
thp_fault_fallback_charge 0
thp_collapse_alloc 4
thp_collapse_alloc_failed 0
thp_file_alloc 0
thp_file_fallback 0
thp_file_fallback_charge 0
thp_file_mapped 0
thp_split_page 0
thp_split_page_failed 0
thp_deferred_split_page 1
thp_split_pmd 1
thp_scan_exceed_none_pte 0
thp_scan_exceed_swap_pte 0
thp_scan_exceed_share_pte 0
thp_split_pud 0
thp_zero_page_alloc 0
thp_zero_page_alloc_failed 0
thp_swpout 0
thp_swpout_fallback 0
balloon_inflate 0
balloon_deflate 0
balloon_migrate 0
swap_ra 0
swap_ra_hit 0
ksm_swpin_copy 0
cow_ksm 0
zswpin 0
zswpout 0
direct_map_level2_splits 29
direct_map_level3_splits 0
nr_unstable 0'

_PROC_VERSION='Linux version 6.2.1 (user@localhost) (gcc (GCC) 12.2.1 20230201, GNU ld (GNU Binutils) 2.40) #1 SMP PREEMPT_DYNAMIC Wed, 01 Mar 2023 00:00:00 +0000'

_PROC_UPTIME='124.08 932.80'

_PROC_STAT='cpu  1957 0 2877 93280 262 342 254 87 0 0
cpu0 31 0 226 12027 82 10 4 9 0 0
cpu1 45 0 664 11144 21 263 233 12 0 0
cpu2 494 0 537 11283 27 10 3 8 0 0
cpu3 359 0 234 11723 24 26 5 7 0 0
cpu4 295 0 268 11772 10 12 2 12 0 0
cpu5 270 0 251 11833 15 3 1 10 0 0
cpu6 430 0 520 11386 30 8 1 12 0 0
cpu7 30 0 172 12108 50 8 1 13 0 0
intr 127541 38 290 0 0 0 0 4 0 1 0 0 25329 258 0 5777 277 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 140223
btime 1680020856
processes 772
procs_running 2
procs_blocked 0
softirq 75663 0 5903 6 25375 10774 0 243 11685 0 21677'

_PROC_LOADAVG='0.12 0.07 0.02 2/165 765'

install_udocker(){

apt update

yes | pkg install -y python-pip patch proot

if pip freeze | grep -Eq "^udocker=="; then
  pip uninstall -y udocker
fi

pip install -U udocker

}

fix_udocker(){

fix_udocker_conf(){
mkdir -p "${HOME}/.udocker/lib"

cat <<'EOF' > "${HOME}/.udocker/udocker.conf"
[DEFAULT]
use_proot_executable = /data/data/com.termux/files/usr/bin/proot
proot_link2symlink = True
verbose_level = 1
EOF

echo "2.9.9" > "${HOME}/.udocker/lib/VERSION"
}

fix_udocker_hardlinks(){
# Fix errors when extracting hardlinks from Docker images
# https://github.com/indigo-dc/udocker/issues/388#issuecomment-1527277800
UDOCKER_PATCH='
--- udocker/container/structure.py
+++ udocker.mod/container/structure.py
@@ -281,7 +281,7 @@
             if Msg.level >= Msg.VER:
                 verbose = '\'v'\''
                 Msg().out("Info: extracting:", tarf, l=Msg.INF)
-            cmd = ["tar", "-C", destdir, "-x" + verbose,
+            cmd = ["proot", "--link2symlink", "tar", "-C", destdir, "-x" + verbose,
                    "--one-file-system", "--no-same-owner", "--overwrite",
                    "--exclude=dev/*", "--exclude=etc/udev/devices/*",
                    "--no-same-permissions", r"--exclude=.wh.*",
'

TMP_PATCH_FILE="$(mktemp)"
patch -p0 --no-backup-if-mismatch -r "${TMP_PATCH_FILE}" -d "$(python -c "import sysconfig; print(sysconfig.get_path('platlib'))" 2>/dev/null || echo "${PREFIX}/lib/python3.11/site-packages")" 2>/dev/null >/dev/null <<< "${UDOCKER_PATCH}" || true
rm -rf "${TMP_PATCH_FILE}"

return 0
}

fix_udocker_qemu(){
# Fix qemu not found errors that occurs when running non-native platform containers
# Not needed for g4f container, but helpful for your other platform containers if any
UDOCKER_PATCH='
--- udocker/engine/base.py
+++ udocker.mod/engine/base.py
@@ -690,4 +690,4 @@
         if not qemu_path:
             Msg().err("Warning: qemu required but not available", l=Msg.WAR)
             return ""
-        return qemu_path if return_path else qemu_filename
+        return qemu_path if return_path else qemu_path
'

TMP_PATCH_FILE="$(mktemp)"
patch -p0 --no-backup-if-mismatch -r "${TMP_PATCH_FILE}" -d "$(python -c "import sysconfig; print(sysconfig.get_path('platlib'))" 2>/dev/null || echo "${PREFIX}/lib/python3.11/site-packages")" 2>/dev/null >/dev/null <<< "${UDOCKER_PATCH}" || true
rm -rf "${TMP_PATCH_FILE}"

return 0
}

fix_udocker_proot(){
# Fix shm errors
# https://github.com/termux/proot-distro/issues/19
# Fix hardcoded bad port errors
#https://github.com/termux/proot-distro/issues/22
UDOCKER_PATCH='
--- udocker/engine/proot.py
+++ udocker.mod/engine/proot.py
@@ -166,7 +166,7 @@
 
         if (Config.conf['proot_link2symlink'] and
                 self._has_option("--link2symlink")):
-            proot_link2symlink = ["--link2symlink", ]
+            proot_link2symlink = ["--link2symlink", "--sysvipc", "-L", "-p", ]
         else:
             proot_link2symlink = []
'

TMP_PATCH_FILE="$(mktemp)"
patch -p0 --no-backup-if-mismatch -r "${TMP_PATCH_FILE}" -d "$(python -c "import sysconfig; print(sysconfig.get_path('platlib'))" 2>/dev/null || echo "${PREFIX}/lib/python3.11/site-packages")" 2>/dev/null >/dev/null <<< "${UDOCKER_PATCH}" || true
rm -rf "${TMP_PATCH_FILE}"

return 0
}

fix_udocker_conf

fix_udocker_hardlinks

fix_udocker_qemu

fix_udocker_proot

return 0
}

proot_write_tmp(){
[ -z "$1" ] && return 1
_TMP_FILE="$(mktemp)"
echo -n "$1" > "$_TMP_FILE"
echo -n "$_TMP_FILE"
return 0
}

udocker_create(){
[ -z "$1" ] && exit 1
[ -z "$2" ] && exit 1
if ! udocker inspect "$1" &>/dev/null; then
  udocker rm "$1" &>/dev/null
  UDOCKER_LOGLEVEL=3 udocker pull "$2" || exit 1
fi
udocker create --name="$1" "$2" &>/dev/null
return 0
}

udocker_prune(){
for i in $(udocker ps | cut -d\  -f1 | tail -n +2); do
  if ! udocker inspect "${i}" &>/dev/null; then
    udocker rm -f "${i}" &>/dev/null || true
  fi
done
return 0
}


udocker_check(){
udocker -V &>/dev/null && command -v patch &>/dev/null && fix_udocker || { install_udocker; fix_udocker; }
}

udocker_run(){
udocker run -v "$(mktemp -d):/dev/shm" -v "$(mktemp -d):/tmp" -v "$(proot_write_tmp "nameserver 8.8.8.8"):/etc/resolv.conf" -v "$(proot_write_tmp "$_PROC_CAP_LAST_CAP"):/proc/sys/kernel/cap_last_cap" -v "$(proot_write_tmp "$_PROC_VM_STAT"):/proc/vmstat" -v "$(proot_write_tmp "$_PROC_VERSION"):/proc/version" -v "$(proot_write_tmp "$_PROC_UPTIME"):/proc/uptime" -v "$(proot_write_tmp "$_PROC_STAT"):/proc/stat" -v "$(proot_write_tmp "$_PROC_LOADAVG"):/proc/loadavg" -v /proc/self/fd/2:/dev/stderr -v /proc/self/fd/1:/dev/stdout -v /proc/self/fd/0:/dev/stdin -v /proc/self/fd:/dev/fd -v /dev/urandom:/dev/random -e QTWEBENGINE_DISABLE_SANDBOX="1" -e MOZ_FAKE_NO_SANDBOX="1" -e XDG_RUNTIME_DIR="/tmp/runtime-root" -e PULSE_SERVER="127.0.0.1" "$@"
}
